//
//  GZStatusRetweetView.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/27.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
import SnapKit
class GZStatusRetweetView: UIView {
    //MARK: -属性
    // 被转发微博视图底部约束
    private var retweetViewBottomConstraint: Constraint?
    var status: GZStatus?{
        didSet{
            let name = status?.user?.name
            let text = status?.text
            let retweetText = "@\(name):\(text)"
            retweetContentLabel.text = retweetText
            
            //卸载约束
            retweetViewBottomConstraint?.uninstall()
            if let prictureURLs = status?.storePictureURLs where prictureURLs.count > 0 {
                retweetPictureView.hidden = false
                retweetPictureView.pictureURLs = prictureURLs
                //设置约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView.snp_bottom).offset(CZStatusCellMargin).constraint
                })
            }else{
                retweetPictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(retweetContentLabel.snp_bottom).offset(CZStatusCellMargin).constraint
                })
            }
        }
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func prepareUI(){
        addSubview(retweetButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        // 约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView.snp_bottom).offset(CZStatusCellMargin).constraint
        }
        
    }
    //MARK: - lazy load
    /// 背景按钮
    private lazy var retweetButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
        }()
    /// 被转发的微博内容
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 12)
        label.numberOfLines = 0
        
        return label
        }()
    //被转发的微博的配图
    private lazy var retweetPictureView: GZStatusPictureView = GZStatusPictureView()
}
